#!/bin/bash

# Slackware build script for kompozer

# Written by Giovanne Castro <giovannefc@ig.com.br>

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=kompozer
VERSION=${VERSION:-0.8b3}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

# No flags/configure needed as it is just a binary repackaging.
ARCH=i686

# If the variable PRINT_PACKAGE_NAME is set, then this script will report what
# the name of the created package would be, and then exit. This information
# could be useful to other scripts.
if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# This should allow you to either pass a different value of KOMPOZERLANG
# when you run the script (KOMPOZERLANG=pt-BR ./kompozer.SlackBuild for
# example) or change it in this script.  Note that you will need to have
# the correct tarball in the build directory for this to work...
# You can get localized tarballs from the following location:
# http://sourceforge.net/projects/kompozer/files/
KOMPOZERLANG=${KOMPOZERLANG:-en-US}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $PKG

# Copy kompozer libraries
mkdir -p $PKG/usr/lib
( cd $PKG/usr/lib
  tar xvf $CWD/kompozer-$VERSION.$KOMPOZERLANG.gcc4.2-$ARCH.tar.gz || exit 1
  mv kompozer kompozer-$VERSION
  ln -sf kompozer-$VERSION kompozer
  cd kompozer-$VERSION
  sed -i -e "s/usr\/local/usr/" kompozer

  cd $PKG/usr/lib/kompozer-$VERSION ) || exit 1

chown -R root:root .
find -L . \
 \( -perm 777 -o -perm 775 -o -perm 750 -o -perm 711 -o -perm 555 -o -perm 511 \) \
 -exec chmod 755 {} \; -o \
 \( -perm 666 -o -perm 664 -o -perm 600 -o -perm 444 -o -perm 440 -o -perm 400 \) \
 -exec chmod 644 {} \;

find $PKG | xargs file | grep -e "executable" -e "shared object" | grep ELF \
  | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

# Create link to executable
mkdir -p $PKG/usr/bin
( cd $PKG/usr/bin
  ln -sf /usr/lib/kompozer-$VERSION/kompozer .
) || exit 1

# Install icons
mkdir -p $PKG/usr/share/applications
cp $CWD/kompozer.desktop $PKG/usr/share/applications/mozilla-kompozer.desktop
mkdir -p $PKG/usr/share/pixmaps
cp $CWD/kompozer.png $PKG/usr/share/pixmaps/kompozer.png

# Move and copy documentation to correct place
mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/lib/kompozer-$VERSION/LICENSE $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/lib/kompozer-$VERSION/bloaturls.txt $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/lib/kompozer-$VERSION/install.log $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/lib/kompozer-$VERSION/dependentlibs.list $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
